package database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReturnTable extends Table {

	private static final String RETURN_INSERT = "INSERT INTO Return VALUES (%s, %s, %s, '%s')";
	private static final String RETURN_DELETE = "DELETE FROM Return WHERE retId=%s";
	private static final String RETURN_DISPLAY = "SELECT * FROM Return";	
	
	private static final String RETURN_EXISTS_QUERY = "SELECT * FROM Return WHERE retId=%s";
		
	private static final String NEXT_RET_ID_QUERY = "SELECT receiptId_counter.nextval FROM dual";
	
	private static final String DATE_FORMAT = "to_date('%s', 'yyyy-mm-dd')";
	
	public enum Column
	{
		RET_ID(1, true),
		DATE(2, false),
		RECEIPT_ID(3, false),
		NAME(4, false);
		
		private int index;
		private boolean isPrimaryKey;
		
		private String[] columnNames = {
				"retId",
				"date",
				"receiptId",
				"name"};
		
		private Column(int index, boolean isPrimaryKey) 
		{
			this.index = index;
			this.isPrimaryKey = isPrimaryKey;
		}
		
		public int getIndex()
		{
			return index;
		}
		
		public String toString()
		{
			return columnNames[this.ordinal()];
		}

		public boolean isPrimaryKey() {
			return this.isPrimaryKey;
		}
	};
	
	public ReturnTable()
	{
		super(RETURN_INSERT, RETURN_DELETE, RETURN_DISPLAY, RETURN_EXISTS_QUERY);
	}

	@Override
	public List<String> getPrimaryKeyValues(List<String> insertValues) {
		List<String> primaryKeyValues = new ArrayList<String>();
		for(Column col : Column.values())
		{
			if(col.isPrimaryKey())
			{	
				primaryKeyValues.add(insertValues.get(col.ordinal()));
			}
		}				
		
		return primaryKeyValues;
	}
	
	@Override
	public List<String> getColumnNames() {
		Column columns[] = Column.values();
		
		List<String> columnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			columnNames.add(col.ordinal(), col.toString());
		}
		return columnNames;
	}

	@Override
	public List<String> getPrimaryKeyColumnNames() {
		Column columns[] = Column.values();
		
		List<String> primaryKeyColumnNames = new ArrayList<String>(columns.length);
		for( Column col : columns)
		{
			if(col.isPrimaryKey())
			{
				primaryKeyColumnNames.add(col.ordinal(), col.toString());
			}
		}
		return primaryKeyColumnNames;
	}
	
	public void createReturn(String date, String receiptId, String storeName) throws SQLException 
	{
		List<String> purchaseTuple = new ArrayList<String>(Column.values().length);
		String dateString = String.format(DATE_FORMAT, date);
		
		int retId = getNextRetId();
		
		purchaseTuple.add(Column.RET_ID.ordinal(), String.valueOf(retId));
		purchaseTuple.add(Column.DATE.ordinal(), dateString);
		purchaseTuple.add(Column.RECEIPT_ID.ordinal(), receiptId);
		purchaseTuple.add(Column.NAME.ordinal(), storeName);
		
		insert(purchaseTuple);
	}
	
	private int getNextRetId() throws SQLException
	{
		int retId = 0;
		
		ResultSet rs = dbCon.executeQuery(NEXT_RET_ID_QUERY);
		if(rs != null)
		{
			if(rs.next())
			{
				retId = rs.getInt(1);
			}
			
			rs.close();
		}
		
		return retId;
	}
	
}
